package com.glodon.paas.storage.ali;

import com.aliyun.common.comm.RequestMessage;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * this class is de-compiled from aliyun SDK, with minor changes to fix a defect
 * 
 * @author guojf
 * 
 */
public class SignUtils {
	private static final String NEW_LINE = "\n";

	// add "group" into SINGED_PARAMETERS
	private static final List<String> SIGNED_PARAMTERS = Arrays
			.asList(new String[]{"acl", "uploadId", "partNumber", "uploads",
					"response-cache-control", "response-content-disposition",
					"response-content-encoding", "response-content-language",
					"response-content-type", "response-expires", "group"});

	public static String buildCanonicalString(String paramString1,
			String paramString2, RequestMessage paramRequestMessage,
			String paramString3) {
		StringBuilder localStringBuilder = new StringBuilder();
		localStringBuilder.append(new StringBuilder().append(paramString1)
				.append("\n").toString());
		Map localMap = paramRequestMessage.getHeaders();
		TreeMap localTreeMap = new TreeMap();
		Map.Entry localEntry;
		String str;
		if (localMap != null) {
			Iterator localIterator = localMap.entrySet().iterator();
			while (localIterator.hasNext()) {
				localEntry = (Map.Entry) localIterator.next();
				if (localEntry.getKey() != null) {
					str = ((String) localEntry.getKey()).toLowerCase();
					if ((str.equals("Content-Type".toLowerCase()))
							|| (str.equals("Content-MD5".toLowerCase()))
							|| (str.equals("Date".toLowerCase()))
							|| (str.startsWith("x-oss-")))
						localTreeMap.put(str, localEntry.getValue());
				}
			}
		}
		if (!localTreeMap.containsKey("Content-Type".toLowerCase()))
			localTreeMap.put("Content-Type".toLowerCase(), "");
		if (!localTreeMap.containsKey("Content-MD5".toLowerCase()))
			localTreeMap.put("Content-MD5".toLowerCase(), "");
		if (paramRequestMessage.getParameters() != null) {
			Iterator localIterator = paramRequestMessage.getParameters()
					.entrySet().iterator();
			while (localIterator.hasNext()) {
				localEntry = (Map.Entry) localIterator.next();
				if (((String) localEntry.getKey()).startsWith("x-oss-"))
					localTreeMap
							.put(localEntry.getKey(), localEntry.getValue());
			}
		}
		Iterator localIterator = localTreeMap.entrySet().iterator();
		while (localIterator.hasNext()) {
			localEntry = (Map.Entry) localIterator.next();
			str = (String) localEntry.getKey();
			Object localObject = localEntry.getValue();
			if (str.startsWith("x-oss-"))
				localStringBuilder.append(str).append(':').append(localObject);
			else
				localStringBuilder.append(localObject);
			localStringBuilder.append("\n");
		}
		localStringBuilder.append(buildCanonicalizedResource(paramString2,
				paramRequestMessage.getParameters()));
		return localStringBuilder.toString();
	}

	private static String buildCanonicalizedResource(String paramString,
			Map<String, String> paramMap) {
		assert (paramString.startsWith("/"));
		StringBuilder localStringBuilder = new StringBuilder();
		localStringBuilder.append(paramString);
		if (paramMap != null) {
			String[] arrayOfString1 = (String[]) paramMap.keySet().toArray(
					new String[paramMap.size()]);
			Arrays.sort(arrayOfString1);
			char c = '?';
			for (String str1 : arrayOfString1)
				if (SIGNED_PARAMTERS.contains(str1)) {
					localStringBuilder.append(c);
					localStringBuilder.append(str1);
					String str2 = (String) paramMap.get(str1);
					if (str2 != null)
						localStringBuilder.append("=").append(str2);
					c = '&';
				}
		}
		return localStringBuilder.toString();
	}
}
